import com.study.beanprocessor.Categroy;
import com.study.entity.Customer;
import com.study.entity.Person;
import com.study.entity.Product;
import com.study.entity.User;
import com.study.service.UserService;
import com.study.util.BeanFactory;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.lang.reflect.Constructor;
import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
 * @Author chenyeqing
 * @Date 2021/9/13 23:23
 */
public class A {

    /**
     * 用于测试：测试Spring的第一个工厂
     */
    @Test
    public void testA() {
        // 1 获得Spring的工厂
        ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
        // 2 通过工厂获得对象
        Person person = (Person) ctx.getBean("person");
        System.out.println(person);
    }

    @Test
    public void testB() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
        // Person person = ctx.getBean("person", Person.class);

        // 获取 Spring工厂配置文件中所有bean标签的id值
        /*String[] beanDefinitionNames = ctx.getBeanDefinitionNames();
        for (String beanDefinitionName : beanDefinitionNames) {
            System.out.println(beanDefinitionName);
        }*/

        // 根据类型获得 Spring配置文件中对应的id值
        /*String[] beanNamesForType = ctx.getBeanNamesForType(Person.class);
        for (String s : beanNamesForType) {
            System.out.println(s);
        }*/

        // containsBeanDefinition 只能判断id，不能判断别名
        System.out.println(ctx.containsBeanDefinition("person"));
        System.out.println(ctx.containsBeanDefinition("p"));
        System.out.println(ctx.containsBeanDefinition("p1"));

        // containsBean 可以判断别名
        System.out.println(ctx.containsBean("person"));
        System.out.println(ctx.containsBean("p"));
        System.out.println(ctx.containsBean("p1"));

    }

    /**
     * 测试没有id会叫什么名字
     */
    @Test
    public void testC() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
        Person bean = ctx.getBean(Person.class);
        System.out.println(bean);

        for (String beanDefinitionName : ctx.getBeanDefinitionNames()) {
            System.out.println(beanDefinitionName);
        }
    }


    @Test
    public void testD() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
        Person p = (Person) ctx.getBean("person");
        System.out.println(p);
    }

    /**
     * 反射创建对象
     */
    @Test
    public void testE() throws Exception{
        Class clazz = Class.forName("com.study.entity.Person");
        Constructor<Person> declaredConstructor = clazz.getDeclaredConstructor(null);
        declaredConstructor.setAccessible(true);
        Person person = declaredConstructor.newInstance(null);
        // Person person = (Person) clazz.newInstance();
        // System.out.println(person);
        System.out.println(person);
    }


    @Test
    public void test01() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
        Person person = (Person) ctx.getBean("person");
        System.out.println(person);
    }

    /**
     * JDK内置类型
     */
    @Test
    public void test02() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
        Person person = (Person) ctx.getBean("person");

        Map<String, String> guns = person.getGuns();
        Set<String> strings = guns.keySet();
        for (String string : strings) {
            System.out.println(string + " -> " + guns.get(string));
        }
        System.out.println("============================");
        for (Object idCard : person.getIdCards()) {
            System.out.println(idCard);
        }
        System.out.println("============================");
        Properties milk = person.getMilk();
        System.out.println("key1 -> " + milk.getProperty("key1"));
        System.out.println("key2 -> " + milk.getProperty("key2"));

        System.out.println("============================");
        for (String name : person.getNames()) {
            System.out.println(name);
        }
        System.out.println("============================");
        for (String qq : person.getQqs()) {
            System.out.println(qq);
        }
    }

    /**
     * 用户自定义类型
     */
    @Test
    public void test03() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
        UserService userService = (UserService) ctx.getBean("userService");
        userService.login("admin", "123");

        userService.register(new User("张三", "123"));

    }

    /**
     * 测试构造方法
     */
    @Test
    public void test04() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
        Customer customer = (Customer) ctx.getBean("customer");
        System.out.println(customer);
    }


    @Test
    public void test05() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
        Connection conn = (Connection) ctx.getBean("conn");
        System.out.println(conn);
    }

    @Test
    public void test06() {
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
        Product product = ctx.getBean("product", Product.class);
        ctx.close();
    }

    @Test
    public void test07() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext1.xml");
        Connection conn = ctx.getBean("conn", Connection.class);
        System.out.println(conn);
    }

    @Test
    public void test08() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext2.xml");
        com.study.converter.Person person = ctx.getBean("person", com.study.converter.Person.class);
        System.out.println(person.getBirthday());
    }

    @Test
    public void test09() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext3.xml");
        Categroy categroy = (Categroy) ctx.getBean("categroy");
        System.out.println(categroy);
    }

}
